<?php
// Display lists of image sources, or lists of projects that used image sources
// List contents vary with user permissions

$relPath='../../pinc/';
require_once $relPath.'dpinit.php';
require_once $relPath.'dp_main.inc';
include_once($relPath.'theme.inc');
// include_once($relPath.'project_states.inc');
// include_once($relPath.'misc.inc'); // array_get()
require_once $relPath."DpTable.class.php";

$which = Arg('which');

$no_stats = 1;

// ---------------------------------------
// Page construction varies with whether the user is logged in or out

if ($User->IsLoggedIn())
{
    if  ( $User->IsImageSourcesManager() ) {
        $min_vis_level = 0;
    } else if ($User->IsProjectManager()) {
        $min_vis_level = 1;
    } else {
        $min_vis_level = 2;
    }
}
else
{
    $min_vis_level = 3;
}


if (!isset($_GET['name']))
{

    $header_text = _("Image Sources");
    theme($header_text, "header");
    echo "<h1>{$header_text}</h1>\n";


    if ($User->IsProjectManager()) {
        echo "<a href='projectmgr.php'>"._("Back to your PM page")."</a><br><br>";
    }

    if ($User->IsLoggedIn()) {
        $tbl = new DpTable();
        $tbl->AddColumn(null, "code_name");
        $tbl->AddColumn(_("Name"), "full_name");
        $tbl->AddColumn(_("Comment"), "public_comment");
        $tbl->AddColumn(_("Publish images?"), "ok_show_images");
        $tbl->AddColumn(_("In Progress"), "active_projects");
        $tbl->AddColumn(_("Completed"), "gold_count");
        $tbl->AddColumn(_("Notes"), "internal_comment");

        $sql = "
            SELECT
                code_name,
                display_name,
                full_name,
                public_comment,
                ok_show_images,
                count(distinct  projectid) AS active_projects,
                0 AS gold_count,
                internal_comment
            FROM image_sources i 
            LEFT JOIN projects p ON i.code_name = p.image_source
            GROUP BY code_name
            ORDER BY display_name";
    } else {
        $tbl->AddColumn(_("Name"), "full_name");
        $tbl->AddColumn(_("Comment"), "public_comment");
        $tbl->AddColumn(_("Publish images?"), "ok_show_images");
        $tbl->AddColumn(_("In Progress"), "active_projects");
        $tbl->AddColumn(_("Completed"), "gold_count");

        $sql = "
            SELECT
                full_name,
                public_comment,
                ok_show_images,
                count(distinct  projectid) = 0 AS active_projects,
                sum(".SQL_CONDITION_GOLD.") AS gold_count
            FROM image_sources i 
            LEFT JOIN projects p ON i.code_name = p.image_source
            GROUP BY code_name
            ORDER BY full_name";
    }

    $rows = $dpdb->SqlRows($sql);

    $tbl->SetRows($rows);
    $tbl->EchoTable();

} else {

    $imso_code = $_GET['name'];

    $imso = mysql_fetch_assoc( mysql_query("
        SELECT
            full_name,
            display_name,
            public_comment,
            internal_comment,
            info_page_visibility,
            concat('<a href=\"',url,'\">',url,'</a>') as 'more_info'
        FROM image_sources
        WHERE code_name = '$imso_code'
    "));

    $visibility = $imso['info_page_visibility'];


    // info page visibility
    //  0 = Image Source Managers and SAs
    //  1 = also any PM
    //  2 = also any logged-in user
    //  3 = anyone

    // layout below intended to make logic easier to follow:
    // see it as a tree lying on it's side, the root node the "or"
    // on the third line

    $can_see = (
            ($visibility == 3)
        or
            ($User->IsLoggedIn()
                and
                (
                    $visibility == 2
                    or
                    ($User->IsProjectManager() and $visibility == 1)
                    or
                    $User->IsImageSourcesManager()
                )
            )
        );

if ($can_see) {

    switch ($which)
    {
        case 'ALL':
            $where_cls = " ";
            $other_which = 'DONE';
            $title = sprintf( _("All Ebooks being produced from scans from %s"), $imso['full_name'] );
            $link_text = sprintf( _("See list of Completed Ebooks produced from scans from %s"), $imso['full_name'] ); 
            break;
        case 'DONE':
        default:
            $where_cls = " AND  ".SQL_CONDITION_GOLD." ";
            $other_which = 'ALL';
            $title = sprintf( _("Completed Ebooks produced from scans from %s"), $imso['full_name'] );
            $link_text = sprintf( _("See list of All Ebooks being produced from scans from %s"), $imso['full_name'] ); 
    }

    $sub_title  = $imso['public_comment'];
    $details    = $imso['internal_comment'];
    $more_info  = $imso['more_info'];

    theme($title, "header");

    echo "<br><h2>$title</h2>\n";

    echo "<br><h3>$sub_title</h3>\n";

    if ($User->IsLoggedIn()) {
        echo "<br><h4>$details</h4>\n";
    }
    echo "\n<h4>$more_info</h4>\n\n";



    echo  "<a href='show_image_sources.php?name=".$imso_code.
        "&which=".$other_which."'>" . $link_text . "</a><br><br>";

    echo  "<a href='show_image_sources.php'>"._("Back to the full listing of all Image Sources")."</a><br><br>";

    $rows = $dpdb->SqlRows("
        SELECT
            concat('<a href=\"$code_url/project.php?projectid=',projectid,'\">',nameofwork,'</a>') as title,
            authorsname as author,
            genre,
            language,
            IF(postednum, concat('<a href=\"http://www.gutenberg.net/etext/',postednum,'\">',postednum,'</a>'),'".mysql_real_escape_string(_('In Progress'))."') as pgid
        FROM projects
        WHERE image_source = '".$imso_code."' ".$where_cls."
        ORDER BY nameofwork");
    
    $tbl = new DpTable();
    $tbl->AddColumn("Title", "title");
    $tbl->AddColumn("Author", "author");
    $tbl->AddColumn("Genre", "genre");
    $tbl->AddColumn("Language", "language");
    $tbl->AddColumn("PG Number", "pgid");

    $tbl->SetRows($rows);
    $tbl->EchoTable();

} else {

    $title = _("Unknown Image Source Code");
    theme($title, "header");

    echo "<br><h2>$title</h2>\n";

    echo  "<a href='show_image_sources.php'>"._("Back to the full listing of Image Sources")."</a><br><br>";

}
}

echo "<br>\n";

theme("","footer");


function in_prog_link($row) {
    return "In progress?";
}

function completed_link($row) {
    return "Completed?";
}
// vim: sw=4 ts=4 expandtab
?>
